package org.sdp.pearproject.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.sdp.pearproject.utils.DateUtil;
import org.springframework.util.StringUtils;

import java.beans.PropertyEditorSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MyListPropertyEditorSupport extends PropertyEditorSupport {

    @Override
    public void setValue(Object value) {
        super.setValue(value);
    }

    @Override
    public Object getValue() {
        return super.getValue();
    }

    @Override
    public void setAsText(String text) {
        if (!StringUtils.hasText(text)) {
            setValue(null);
            return;
        }
        try {
            JSONArray jsonArray = JSON.parseArray(text);
            if (jsonArray.size() == 0) {
                setValue(new ArrayList<>());
                return;
            }
            Object v = jsonArray.get(0);
            if (v instanceof String
                    || v instanceof Integer
                    || v instanceof Long
            ) {
                List l = new ArrayList<>();
                l.addAll(jsonArray);
                setValue(l);
                return;
            }
        }catch(Exception ex) {
            ex.printStackTrace();
        }
        throw new java.lang.IllegalArgumentException("cannot set list by text:"+text);
    }

    @Override
    public String getAsText() {
        return super.getAsText();
    }
}